<template>
  <!-- 搜索框组件 -->
  <div class="home">
    <span class="time">{{ currentTime }}</span>
    <div class="search_box">
      <div class="search_x" :style="{ width: iptwidth + 'px' }">
        <input
          type="text"
          v-model="value"
          class="search_ipt"
          @keydown.enter="openlinks(value)"
          @blur="narrow"
          @mousemove="x"
        />
        <span
          class="iconfont icon-sousuo4"
          @mousemove="x"
          @click="openlinks(value)"
        ></span>
      </div>
    </div>
    <div class="drop-box">
      <div class="list-box" :style="{ width: listwidth + 'px' }">
        <ul class="search_list">
          <li
            v-for="(item, index) in data"
            :key="index"
            class="search_tags"
            @click="openlinks(item.q)"
          >
            {{ item.q }}
          </li>
        </ul>
      </div>
    </div>
  </div>
</template>

<script>
export default {
  name: "SearchBox",
  data() {
    return {
      value: "",
      iptwidth: "235",
      listwidth: "0",
      data: {},
      timer: "",
      currentTime: "",
    };
  },
  methods: {
    x() {
      this.iptwidth = "435";
    },
    openlinks(val) {
      if (val) {
        window.open(`https://www.baidu.com/s?wd=${val}`);
      }
    },
    narrow() {
      if (!this.value) {
        this.iptwidth = "235";
        this.listwidth = "0";
      }
    },
    check(i) {
      const num = i < 10 ? "0" + i : i;
      return num;
    },
  },
  created() {
    var _this = this;
    this.timer = setInterval(function () {
      let hours = new Date().getHours();
      let minutes = new Date().getMinutes();
      if (hours < 10) {
        hours = "0" + hours;
      }
      if (minutes < 10) {
        minutes = "0" + minutes;
      }
      _this.currentTime = hours + ":" + minutes;
    }, 1000);
  },
  beforeDestroy() {
    if (this.timer) {
      clearInterval(this.timer);
    }
  },
  watch: {
    async value(val) {
      if (val != "") {
        const data = await this.$jsonp(
          `https://www.baidu.com/sugrec?prod=pc&cb=getData&wd=${val}`,
          {
            callbackQuery: "cb",
            callbackName: "jsonp_func",
          }
        );

        this.data = data.g;
        this.listwidth = "420";
      } else this.listwidth = "0";
    },
  },
};
</script>

<style>
.time {
  position: fixed;
  top: 60px;
  left: 47%;
  font-size: 30px;
  cursor: pointer;
  color: #fff;
  transition: 0.3s;
}
.time:hover {
  transform: scale(1.2);
}
.search_box {
  position: fixed;
  display: flex;
  flex-wrap: nowrap;
  top: 130px;
  left: 50%;
  transform: translate(-50%, -50%);
  border-radius: 35px;
  height: 35px;
  background: #fff;
}
.search_x {
  /* width: 450px; */
  transition: 0.4s;
  position: relative;
}

.search_ipt {
  border: none;
  background: none;
  outline: none;
  float: left;
  padding: 0;
  color: black;
  font-size: 16px;
  transition: 0.4s;
  line-height: 35px;
  width: 450px;
  margin-top: -1px;
  text-align: center;
  font-size: normal;
  font-weight: 400;
}
.icon-sousuo4 {
  position: absolute;
  right: 0;
  color: #46a3ff;
  font-size: 18px;
  font-weight: 700;
  float: right;
  width: 35px;
  height: 35px;
  border-radius: 50%;
  background: white;
  display: flex;
  justify-content: center;
  align-items: center;
}
.icon-sousuo4:hover {
  color: #ff8000;
  cursor: pointer;
}
.drop-box {
  height: 360px;
  position: fixed;
  top: 330px;
  left: 50%;
  transform: translate(-50%, -50%);
}
.search_list {
  width: 100%;
  border-radius: 20px;
  background: rgba(0, 0, 0, 0.2);
  margin: 0;
  padding: 0;
  overflow: hidden;
  font-size: normal;
  font-weight: 500;
}
.search_tags {
  list-style: none;
  width: 100%;
  height: 35px;
  color: rgba(255, 255, 255, 0.8);
  line-height: 35px;

  cursor: pointer;
}
.search_tags:hover {
  background: rgba(255, 255, 255, 0.2);
}
</style>
